#include "StdAfx.h"
#include "user_reg_handler.h"

namespace fly {

	namespace jld{

		namespace server {

			user_reg_handler::user_reg_handler()
				:user_handler()
			{
			}

			bool user_reg_handler::handle_user(User& user, recv_buf& recv_buf, send_buf& send_buf, conn_info& conn_info)
			{
				if (CREATE != user.action())
				{
					return user_handler::handle_user(user, recv_buf, send_buf, conn_info);
				}
				LOG_INFO("user register handler");

				if (!check_account(user))
				{
					return false;
				}
				Ack ack;
				ack.set_what(USER);
				ack.set_action(user.action());
				ack.set_success(true);

				if (!ucache->add(user))
				{
					LOG_DEBUG("This account has been occupied:", user.account());
					ack.set_success(false);
					ack.set_msg("This account has been occupied");
					return send_buf.serialize_to_send(ACK, target_, ack);
				}

				send_buf.serialize_to_send(ACK, target_, ack);
				//transmit rtt handler
				recv_buf.pid() = RTT;
				//record user info
				conn_info.set_account(user.account());
				return true;
			}
		}
	}
}
